﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using AjaxControlToolkit;
using BizElements.Core;
using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>Represents bindings between child collection and <see cref="ICollectionEditor"/> user control.</summary>
    public sealed class ChildrenBinding : IChildrenBinding
    {
        #region Members.

        ICollectionEditor collectionEditor;

        #endregion

        #region CTor.

        /// <summary>Initializes a new instance of <b>ChildrenBinding</b> class.</summary>
        /// <param name="propertyName">Name of child collection property.</param>
        /// <param name="collectionEditor">Collection editor to which child collection is bound.</param>
        /// <param name="childFormUrl">Optional child form URL.</param>
        public ChildrenBinding(string propertyName, ICollectionEditor collectionEditor, string childFormUrl)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new ArgumentNullException("propertyName");
            if (collectionEditor == null)
                throw new ArgumentNullException("collectionEditor");

            this.ChildrenPropertyName = propertyName;
            this.collectionEditor = collectionEditor;
            this.ChildFormUrl = childFormUrl;
        }

        #endregion

        #region Properties.

        /// <summary>Gets the name of child collection property.</summary>
        public string ChildrenPropertyName { get; private set; }

        /// <summary>Gets optional child form URL.</summary>
        public string ChildFormUrl { get; private set; }

        /// <summary>Gets or sets tab panel index that contains collection editor.</summary>
        public int TabIndex { get; set; }

        /// <summary>Gets ID of collection editor control.</summary>
        public string CollectionEditorId
        {
            get { return this.collectionEditor.ID; }
        }

        #endregion

        #region Methods.

        /// <summary>Gets collection editor control.</summary>
        /// <param name="parentBizobjControl">Parent control.</param>
        public ICollectionEditor GetCollectionEditor(Control parentBizobjControl)
        {
            return this.collectionEditor;
        }

        #endregion
    }
}
